package com.tusuo.stds.ui.scenedetail;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.SceneDetail;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.suggest.EmptyPresenter;
import com.tusuo.stds.ui.suggest.EmptyView;

import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

public class ModuleDetailActivity extends BaseToolbarActivity<EmptyView,EmptyPresenter> {

    GridView gvLevelList;
    private SceneDetail.SceneModule module;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module_detail);

        Intent intent = getIntent();
        if (intent.getExtras() != null) {
            module = Parcels.unwrap(intent.getParcelableExtra("module"));
        }

        initData();

        adapter = new MyAdapter();
        gvLevelList = (GridView) findViewById(R.id.module_detail_gv);

        gvLevelList.setAdapter(adapter);

        gvLevelList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SceneDetail.SceneLevel level = (SceneDetail.SceneLevel) adapter.getItem(position);
                Intent intent1 = new Intent(ModuleDetailActivity.this, LevelDetailActivity_.class);
                intent1.putExtra("level",Parcels.wrap(level));
                startActivity(intent1);
            }
        });
    }

    private void initData() {
        setupUI(this);
        ToolbarOwner.Config config = new ToolbarOwner.Config((module != null) ? module.Title : "模块名称", true, R.drawable.ic_back_light, null);
        mToolbarOwner.setConfig(config);
    }

    @NonNull
    @Override
    public EmptyPresenter createPresenter() {
        return new EmptyPresenter();
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (module != null) {
                if (module.SceneLevelList != null) {
                    return module.SceneLevelList.size();
                }else {
                    return 0;
                }
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (module != null) {
                if (module.SceneLevelList != null) {
                    return module.SceneLevelList.get(position);
                }else {
                    return null;
                }
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder holder;
            if (convertView == null) {
                view = View.inflate(ModuleDetailActivity.this,R.layout.module_detail_list_item,null);
                holder = new ViewHolder();
                holder.tvTitle = (TextView) view.findViewById(R.id.module_detail_item_tv_title);
                view.setTag(holder);
            }else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }

            if (module != null) {
                if (module.SceneLevelList != null) {
                    holder.tvTitle.setText(module.SceneLevelList.get(position).Title);
                }else {
                    holder.tvTitle.setText("");
                }
            }else {
                holder.tvTitle.setText("");
            }

            return view;
        }
    }

    private class ViewHolder {
        TextView tvTitle;
    }

}
